package com.example.administrator.szb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.SearchBean;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by stone on 2018/1/1.
 */

public class AskFragmentAdapter2 extends android.widget.BaseAdapter {
    private Context context;
    private List<SearchBean.DataBean.ReferListBean> list;
    private LayoutInflater inflater;
    public AskFragmentAdapter2(Context context, List list) {
        this.context = context;
        this.list = list;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public SearchBean.DataBean.ReferListBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.item_wd1,null);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.reset();
        SearchBean.DataBean.ReferListBean bean  = getItem(position);
        holder.showBusiness(bean.getBusiness());
        holder.ask_name.setText(bean.getName());
        holder.ask_time.setText(bean.getCreate_time());
        Glide.with(context).load(bean.getHeadimg()).error(R.mipmap.default_header_s).into(holder.ask_headimg);
        holder.ask_titles.setText(bean.getTitle());
        holder.ask_content.setText(bean.getContent());
        holder.ask_shoucang.setText(bean.getCollect_count()+"收藏");
        holder.ask_huida.setText(bean.getAnswer_count()+"回答");
        return convertView;
    }

     class ViewHolder{
         TextView ask_huida,ask_shoucang,ask_name,ask_time,ask_guanhzu,ask_titles,ask_content,ask_business1,ask_business2
                 ,ask_business3,ask_business4;
         LinearLayout ll_ask_business;
         ImageView ask_headimg;
        public ViewHolder(View view){
            bindViews(view);

        }
         public void reset(){
             ask_guanhzu.setVisibility(View.GONE);
             ll_ask_business.setVisibility(View.GONE);
             ask_business1.setVisibility(View.GONE);
             ask_business2.setVisibility(View.GONE);
             ask_business3.setVisibility(View.GONE);
             ask_business4.setVisibility(View.GONE);
         }
         public void showBusiness(List<SearchBean.DataBean.ReferListBean.BusinessBean> list){
             ll_ask_business.setVisibility(View.VISIBLE);
             for (int i=0;i<list.size();i++){
                 switch (i){
                     case 0:
                         ask_business1.setText(list.get(i).getTitle());
                         ask_business1.setVisibility(View.VISIBLE);
                     break;
                     case 1:

                         ask_business2.setText(list.get(i).getTitle());
                         ask_business2.setVisibility(View.VISIBLE);
                         break;
                     case 2:

                         ask_business3.setText(list.get(i).getTitle());
                         ask_business3.setVisibility(View.VISIBLE);
                         break;
                     case 3:

                         ask_business4.setText(list.get(i).getTitle());
                         ask_business4.setVisibility(View.VISIBLE);
                         break;
                 }
             }
         }
         private void bindViews(View view) {

             ask_headimg = (CircleImageView) view.findViewById(R.id.ask_headimg);
             ask_name = (TextView) view.findViewById(R.id.ask_name);
             ask_time = (TextView) view.findViewById(R.id.ask_time);
             ask_guanhzu = (TextView) view.findViewById(R.id.ask_guanhzu);
             ask_titles = (TextView) view.findViewById(R.id.ask_titles);
             ask_content = (TextView) view.findViewById(R.id.ask_content);
             ll_ask_business = (LinearLayout) view.findViewById(R.id.ll_ask_business);
             ask_business1 = (TextView) view.findViewById(R.id.ask_business1);
             ask_business2 = (TextView) view.findViewById(R.id.ask_business2);
             ask_business3 = (TextView) view.findViewById(R.id.ask_business3);
             ask_business4 = (TextView) view.findViewById(R.id.ask_business4);
             ask_shoucang = (TextView) view.findViewById(R.id.ask_shoucang);
             ask_huida = (TextView) view.findViewById(R.id.ask_huida);
         }
    }
}
